using System.Web.UI.HtmlControls;
using System;
using System.Configuration;
using System.Drawing;
using System.Data;
using System.Web.UI.WebControls;
using Microsoft.VisualBasic;
using System.Collections;
using System.Web.UI;
using System.Web;


using System.IO;
using System.Text;
//using com.ddsc.database;
using DDSC.OracleADO;
using com.ddsc.common;
using SMAS;
using System.Resources;

namespace smasb2b
{
    public class PurchaseBulletin : System.Web.UI.UserControl
    {

        ArrayList m_arrParam = new ArrayList();
        dbAccess dba = new dbAccess();


        #region  Web Form 設計工具產生的程式碼

        //此為 Web Form 設計工具所需的呼叫。
        [System.Diagnostics.DebuggerStepThrough()]
        private void InitializeComponent()
        {

        }
        protected System.Web.UI.WebControls.Label lblNOTIC_DATE;
        protected System.Web.UI.WebControls.Label lblNOTIC_TEXT;
        protected System.Web.UI.WebControls.Label lblPAGEINDEX;
        protected System.Web.UI.WebControls.Label lblPAGE1;
        protected System.Web.UI.WebControls.Label lblTOTAL;
        protected System.Web.UI.WebControls.Label lblPAGE;
        protected System.Web.UI.WebControls.ImageButton btnPREV;
        protected ddscControlLibrary.InputBoxCtrl GotoPage;
        protected System.Web.UI.WebControls.ImageButton BtnGo;
        protected System.Web.UI.WebControls.ImageButton BtnNext;
        protected System.Web.UI.HtmlControls.HtmlTable DataGrid;
        protected System.Web.UI.HtmlControls.HtmlTableRow DataGrid_foot;
        protected ddscControlLibrary.InputBoxCtrl PageIndex;
        protected ddscControlLibrary.InputBoxCtrl PageCount;
        protected System.Web.UI.WebControls.Label lblNOTICE_TEXT;
        protected System.Web.UI.HtmlControls.HtmlTable VendorBulletin;
        protected System.Web.UI.HtmlControls.HtmlTableRow VendorBulletin2;
        protected System.Web.UI.WebControls.Label lblPURCHASETITLE;
        protected System.Web.UI.WebControls.Label lblVENDERENTER;
        protected System.Web.UI.WebControls.Label lblADDNEWCODE;
        protected System.Web.UI.WebControls.Label lblNOTICE_DATE;
        protected System.Web.UI.HtmlControls.HtmlTableRow tr1;
        protected System.Web.UI.WebControls.Label lblVENDERENTER1;

        //注意: 下列預留位置宣告是 Web Form 設計工具需要的項目。
        //請勿刪除或移動它。
        private System.Object designerPlaceholderDeclaration;

        private void Page_Init(System.Object sender, System.EventArgs e)
        {
            //CODEGEN: 此為 Web Form 設計工具所需的方法呼叫
            //請勿使用程式碼編輯器進行修改。
            InitializeComponent();
        }

        #endregion
        public string Login;
        private void Page_Load(System.Object sender, System.EventArgs e)
        {
            //在這裡放置使用者程式碼以初始化網頁
            VENDOR LoginUser = default(VENDOR);
            if (Session["LoginUser"] == null)
            {
                Login = "N";
                tr1.Visible = true;
            }
            else
            {
                LoginUser = (VENDOR)Session["LoginUser"];
                if (LoginUser.VENDOR_STATUS == "9")
                {
                    Login = LoginUser.VENDOR_STATUS;
                }
                else
                {
                    Login = LoginUser.B2B_FLAG_STATUS;
                }

                tr1.Visible = false;
            }

            PageIndex.Text = System.Convert.ToString(1);
            DataGrid_DataBind();
        }

        private void btnPREV_Click(System.Object sender, System.Web.UI.ImageClickEventArgs e)
        {
            PageIndex.Text = ((int.Parse(PageIndex.Text)) - 1).ToString();
            DataGrid_DataBind();
        }

        private void BtnNext_Click(System.Object sender, System.Web.UI.ImageClickEventArgs e)
        {
            PageIndex.Text = ((int.Parse(PageIndex.Text)) + 1).ToString();
            DataGrid_DataBind();
        }

        public void DataGrid_DataBind()
        {
            //變數宣告
            //Dim LoginUser As VENDOR = GetLoginUser()
            DataSet ds = default(DataSet);
            //dbAccess dba = new dbAccess();
            string strCondition = "";
            int i;
            //Dim count As Integer = 3
            //Dim str_SQL_a(count) As String
            //Dim str_SQL_b(count) As String
            string SystemDate = DateTime.Now.ToString("yyyyMMdd");
            try
            {
                //目前頁次指定Goto
                GotoPage.Text = Server.HtmlEncode(PageIndex.Text);
               
                //預設查詢語法				
                string SqlCommandCount = "";
                SqlCommandCount = "select  count(tb1.purchase_no) As row_total_count from b2b_purchase_notice tb1,(select purchase_no,max(version) as version ";
                SqlCommandCount = SqlCommandCount + " from b2b_purchase_notice where status <> \'0\'   group by purchase_no) tb2 ";
                //SqlCommandCount = SqlCommandCount + " where tb1.purchase_no = tb2.purchase_no And tb1.version = tb2.version AND STATUS=\'9\' and start_date<=\'" + SystemDate + "\'and end_date >=\'" + SystemDate + "\'";
                //Adams : 1.修改SQL語法使用參數，不組字串
                SqlCommandCount = SqlCommandCount + " where tb1.purchase_no = tb2.purchase_no And tb1.version = tb2.version AND STATUS=\'9\' and start_date <= :SystemDate and end_date >=:SystemDate";

                // 查詢資料內容
                string SqlCommandText = "";
                SqlCommandText = "select  start_date NOTICE_DATE ,purchase_text NOTICE_TEXT, tb1.purchase_no,tb1.version ";
                SqlCommandText = SqlCommandText + " from b2b_purchase_notice tb1,(select purchase_no,max(version) as version from b2b_purchase_notice ";
                SqlCommandText = SqlCommandText + " where status <> \'0\'  group by purchase_no) tb2 where tb1.purchase_no = tb2.purchase_no And tb1.version = tb2.version ";
                //SqlCommandText = SqlCommandText + " AND STATUS=\'9\' and start_date<=\'" + SystemDate + "\' and end_date > \'" + SystemDate + "\'";
                SqlCommandText = SqlCommandText + " AND STATUS=\'9\' and start_date <= :SystemDate and end_date > :SystemDate";


                int intCount = 0;
                int size = 20; // Update by titan 由BasePage設定

                // 計算資料總筆數
                //DataSet dsCount = dba.runExecuteQuery("Vendor_Count", SqlCommandCount);
                //Adams : 2.設定動態參數物件
                m_arrParam.Clear();
                m_arrParam.Add(dba.GetOracleParam(":SystemDate", SystemDate));
                //Adams : 3.修改執行方法Overload
                DataSet dsCount = dba.runExecuteQuery("Vendor_Count", SqlCommandCount, m_arrParam);

                if (dsCount.Tables[0].Rows[0]["ReturnCode"] == "000")
                {
                    intCount = int.Parse(System.Convert.ToString(dsCount.Tables["Vendor_Count"].Rows[0]["row_total_count"]));
                    PageCount.Text = (Math.Ceiling(System.Convert.ToDecimal(double.Parse((intCount).ToString()) / size))).ToString();
                }

                //Update By Titan 處理當總頁次小於目前頁次
                if ((int.Parse(PageCount.Text) < int.Parse(PageIndex.Text)) && int.Parse(PageCount.Text) > 0)
                {
                    // 總筆數(intCount)小於一頁，從第0筆開始顯示
                    PageIndex.Text = Server.HtmlEncode(PageCount.Text);
                    GotoPage.Text = Server.HtmlEncode(PageCount.Text);
                }
                // Update by titan 由BasePage設定
                // absolute position
                int position = ((int.Parse(PageIndex.Text)) - 1) * size;
                int maxrecord = position + size;


                //控制分頁按鈕
                ControlPageButton1(DataGrid_foot);

                //執行資料庫語法                
                //ds = dba.runExecuteQuery("b2b_purchase_notice", SqlCommandText + strCondition, size, position);
                //使用新物件OracleADO
                ds = dba.runExecuteQuery("b2b_purchase_notice", SqlCommandText + strCondition, position , size, m_arrParam);

                if (!(ds == null) && ds.Tables["info"].Rows[0]["ReturnCode"] != "100" && ds.Tables["info"].Rows[0]["ReturnCode"] != "999")
                {
                    HtmlTableRow tr = (HtmlTableRow)(DataGrid.FindControl("DataGrid_head"));
                    ArrayList ArrKey = new ArrayList();
                    ArrKey.Add("NOTICE_DATE");
                    ArrKey.Add("NOTICE_TEXT");
                    ArrKey.Add("purchase_no");
                    ArrKey.Add("version");

                    DataGrid_Bind_PurchaseBulletin(DataGrid, ArrKey, ds.Tables["b2b_purchase_notice"]);                   
                }
                else
                {
                    this.DataGrid_foot.Visible = false;
                }

            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                ds.Dispose();
            }
        }

        public void ControlPageButton1(HtmlTableRow DataGrid_Foot)
        {
            //Dim FirstButton As System.Web.UI.WebControls.ImageButton = CType(DataGrid_Foot.FindControl("btnFirst"), System.Web.UI.WebControls.ImageButton)
            System.Web.UI.WebControls.ImageButton PrevButton = (System.Web.UI.WebControls.ImageButton)(DataGrid_Foot.FindControl("btnPrev"));
            System.Web.UI.WebControls.ImageButton NextButton = (System.Web.UI.WebControls.ImageButton)(DataGrid_Foot.FindControl("btnNext"));
            // Dim LastButton As System.Web.UI.WebControls.ImageButton = CType(DataGrid_Foot.FindControl("btnLast"), System.Web.UI.WebControls.ImageButton)
            // Dim GoButton As System.Web.UI.WebControls.ImageButton = CType(DataGrid_Foot.FindControl("btnGo"), System.Web.UI.WebControls.ImageButton)
            ddscControlLibrary.InputBoxCtrl PageIndex = (ddscControlLibrary.InputBoxCtrl)(DataGrid_Foot.FindControl("PageIndex"));
            ddscControlLibrary.InputBoxCtrl PageCount = (ddscControlLibrary.InputBoxCtrl)(DataGrid_Foot.FindControl("PageCount"));
            int count = int.Parse(PageCount.Text);
            int index = int.Parse(PageIndex.Text);

            // .FirstButton.Enabled = True
            PrevButton.Enabled = true;
            NextButton.Enabled = true;
            // .LastButton.Enabled = True
            // GoButton.Enabled = True
            if (count == index)
            {
                NextButton.Enabled = false;
                // .LastButton.Enabled = False
            }
            if (index == 1)
            {
                // FirstButton.Enabled = False
                PrevButton.Enabled = false;
            }
            if (count == 0)
            {
                // FirstButton.Enabled = False
                PrevButton.Enabled = false;
                NextButton.Enabled = false;
                // LastButton.Enabled = False
                //GoButton.Enabled = False
                PageIndex.EnableView = true;
            }

        }

        public void DataGrid_Bind_PurchaseBulletin(HtmlTable DataGrid, ArrayList ArrKey, DataTable DataSource)
        {
            HtmlTableRow tr = (HtmlTableRow)(DataGrid.FindControl("DataGrid_head"));
            int i = 0;
            int j = 0;
            int k = 0;
            string Url = "";
            string className = "";
            ResourceManager rm = GetResourceManager();
            for (i = 0; i <= DataSource.Rows.Count - 1; i++)
            {
                HtmlTableRow htr = new HtmlTableRow();
                if (i % 2 == 0)
                {
                    className = "evenRow";
                }
                else
                {
                    className = "oddRow";
                }
                htr.Attributes.Add("class", className);
                DataGrid.Rows.Add(htr);
                for (j = 0; j <= tr.Cells.Count - 1; j++)
                {
                    HtmlTableCell hd = new HtmlTableCell();

                    switch (tr.Cells[j].Attributes["class"].ToLower())
                    {
                        case "link":
                            Url = string.Format("{0}?action_type={1}&GUID={2}", tr.Cells[j].Attributes["url"].ToString(), tr.Cells[j].ID, Guid.NewGuid());
                            for (k = 1; k <= ArrKey.Count - 1; k++)
                            {
                                Url += "&" + ArrKey[k] + "=" + DataSource.Rows[i][ArrKey[k].ToString()].ToString();
                            }
                            string String1 = "dialogWidth=740px;dialogHeight=740px;center=yes;border=thin;help=no;status=no;resizable=no;";
                            hd.Width = tr.Cells[j].Width;
                            //<!-- 'Update By Ben 2008/4/21 S8970025 -->
                            hd.InnerHtml = "<a href=\"javascript:var Login=\'" + Login + "\';if(Login!=\'9\' ){ if(Login==\'N\' ){alert(GetMessageByID(\'MSG066\').text);var returnvalue= window.showModalDialog(\'/smasb2b/Login_pop.aspx?action_type=ADD\',window,\'dialogWidth=230PX;dialogHeight=290px;center=yes;border=thin;help=no;status=no;resizable=no;\', true)} else if(parseInt(Login)<1){ alert(GetMessageByID(\'MSG067\').text)}else{window.showModalDialog(\'" + Url + "\',null,\'" + String1 + "\', true)};if(returnvalue!=null){window.location=\'/smasb2b/default.aspx?ID=\'+returnvalue[0]+\'&PW=\'+returnvalue[1];}}else{ window.showModalDialog(\'" + Url + "\',null,\'" + String1 + "\', true)}\">" + DataSource.Rows[i][tr.Cells[j].Controls[1].ID.Replace("lbl", "")].ToString() + "</a>";
                            break;
                        // hd.InnerHtml = "<a href=""javascript:var Login='" + Login + "';if(Login!='9' ){ if(Login=='N' ){alert(GetMessageByID('MSG066').text);var returnvalue= window.showModalDialog('/smasb2b/Login_pop.aspx?action_type=ADD',window,'dialogWidth=230PX;dialogHeight=290px;scrollbars=no;center=yes;border=thin;help=no;status=no;resizable=no;', true)} else if(parseInt(Login)<1){ alert(GetMessageByID('MSG067').text)}else{window.open('" + Url + "')};if(returnvalue!=null){window.location='/smasb2b/default.aspx?ID='+returnvalue[0]+'&PW='+returnvalue[1];}}else{ window.open('" + Url + "')}"">" + DataSource.Rows(i)(tr.Cells(j).Controls(1).ID.Replace("lbl", "")).ToString() + "</a>"

                        //hd.InnerHtml = "<a onclick=""PURCHASETITLE('" + Login + "','" + Url + "');"" href="""" style=""FONT-SIZE: 12px"">" + DataSource.Rows(i)(tr.Cells(j).Controls(1).ID.Replace("lbl", "")).ToString() + "</a>"

                        case "tdtitle":
                            hd.Width = tr.Cells[j].Width;
                            hd.Attributes.Add("class", "detail");
                            hd.InnerText = DataSource.Rows[i][tr.Cells[j].Controls[1].ID.Replace("lbl", "")].ToString();
                            break;
                        default:
                            hd.Width = tr.Cells[j].Width;
                            hd.InnerText = rm.GetString(tr.Cells[j].ID);
                            break;
                    }
                    DataGrid.Rows[DataGrid.Rows.Count - 1].Cells.Add(hd);

                }
            }

        }

        public ResourceManager GetResourceManager()
        {
            string oldChar;
            string newChar;
            if (System.Convert.ToString(Session["langauage"]) == "zh-tw")
            {
                oldChar = "eng";
                newChar = "chin";
            }
            else
            {
                oldChar = "chin";
                newChar = "eng";
            }
            return new ResourceManager("smasb2b." + (string)Session["langauage"], System.Reflection.Assembly.GetExecutingAssembly());
        }

    }

}
